package net.kem.android.positioner;

import android.util.Log;

// 1;2.26;0507362270
public class ServerResponse {
	private byte code;
	private int gpsRefreshDistance;
	private String clientID;

	private static final ServerResponse ERROR_RESPONSE = new ServerResponse((byte)-1, Integer.MIN_VALUE, "");
	private ServerResponse(byte code, int gpsRefreshDistance, String clientID) {
		this.code = code;
		this.gpsRefreshDistance = gpsRefreshDistance;
		this.clientID = clientID;
	}

	static ServerResponse parseServerResponse(String response) {
		ServerResponse res;
		if(response != null && response.length() > 0) {
			String[] split = response.split(";");
			if(split.length == 3) {
				try {
					byte code = Byte.parseByte(split[0]);
//					int gpsRefreshDistance = Integer.parseInt(split[1]);
					float fGpsRefreshDistance = Float.parseFloat(split[1]);
					int gpsRefreshDistance = (int)(fGpsRefreshDistance * 1000);
					String clientID = split[2];
					res = new ServerResponse(code, gpsRefreshDistance, clientID);
					return res;
				} catch (NumberFormatException e) {
					Log.e("parseServerResponse", "Invalid server response: " + response);
				}
			}

		}
		return ERROR_RESPONSE;
	}

	byte getCode() {
		return code;
	}

	void setCode(byte code) {
		this.code = code;
	}

	int getGpsRefreshDistance() {
		return gpsRefreshDistance;
	}

	void setGpsRefreshDistance(int gpsRefreshDistance) {
		this.gpsRefreshDistance = gpsRefreshDistance;
	}

	String getClientID() {
		return clientID;
	}

	void setClientID(String clientID) {
		this.clientID = clientID;
	}

	static ServerResponse getErrorResponse() {
		return ERROR_RESPONSE;
	}
}